import { defineStore } from 'pinia'
import { ILoginType, ISession } from '@/types/auth'
import { loginAdminApi, loginHomeApi } from '@/apis/session'
import { ElMessage } from 'element-plus'
import { emailPattern, phonePattern } from '@/utils/element/elRules'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    loginForm: {
      username: '',
      password: '',
      isRemember: false
    },
    phoneLoginForm: {
      phoneNumber: '',
      code: ''
    },
    emailLoginForm: {
      email: '',
      code: ''
    },
    loginType: 'common' as ILoginType,
    loginLoading: false,
    loginAdminLoading: false,
    isVerified: false,
    isGettingCode: false,
    getCodeCountdown: 60,
    getCodeTimer: null as NodeJS.Timeout | null,
    homeSession: {} as ISession
  }),
  actions: {
    changeLoginType(type: ILoginType) {
      this.loginType = type

      if (type === 'common') {
        this.stopCountdown()
      }
      this.resetLoginForm()
    },
    getAuthCode(type: ILoginType) {
      switch (type) {
        case 'phone':
          if (phonePattern.test(this.phoneLoginForm.phoneNumber)) {
            this.isGettingCode = true
            ElMessage.success('验证码已发送至手机')
            this.startCountdown()
          } else {
            ElMessage.error('请先输入正确的手机号')
          }
          break
        case 'email':
          if (emailPattern.test(this.emailLoginForm.email)) {
            this.isGettingCode = true
            ElMessage.success('验证码已发送至邮箱')
            this.startCountdown()
          } else {
            ElMessage.error('请先输入正确的邮箱地址')
          }
          break
        default:
          break
      }
    },
    startCountdown() {
      this.getCodeTimer = setInterval(() => {
        if (this.getCodeCountdown > 0) {
          this.getCodeCountdown--
        } else {
          clearInterval(this.getCodeTimer as NodeJS.Timeout)
          this.isGettingCode = false
          this.getCodeCountdown = 60
        }
      }, 1000)
    },
    stopCountdown() {
      if (this.getCodeTimer) {
        clearInterval(this.getCodeTimer)
      }
    },
    async handleLogin(type: ILoginType) {
      const { username, password } = this.loginForm
      const { phoneNumber, code: phoneCode } = this.phoneLoginForm
      const { email, code: emailCode } = this.emailLoginForm
      switch (type) {
        case 'common':
          this.homeSession = await loginHomeApi(username, password)
          break
        case 'phone':
          this.homeSession = await loginHomeApi(phoneNumber, phoneCode)
          break
        case 'email':
          this.homeSession = await loginHomeApi(email, emailCode)
          break
        case 'admin':
          this.homeSession = await loginAdminApi(username, password)
          break
        default:
          break
      }
      ElMessage.success('登录成功')
    },
    resetLoginForm() {
      switch (this.loginType) {
        case 'common':
          this.loginForm = {
            username: '',
            password: '',
            isRemember: false
          }
          break
        case 'phone':
          this.phoneLoginForm = {
            phoneNumber: '',
            code: ''
          }
          break
        case 'email':
          this.emailLoginForm = {
            email: '',
            code: ''
          }
          break
        default:
          break
      }
    }
  }
})
